<?php
/* 
* shipped orders
 */
class ShippedAction extends CommonAction {
    public function _filter(&$map) {
      if( $_REQUEST['remark'] != '' )
          $map['remark'] = trim($_REQUEST['remark']);

        if( $_REQUEST['logino'] != '' )
          $map['logino'] = array('like',"%".trim($_REQUEST['logino'])."%");

        if( $_REQUEST['phone'] != '' )
          $map['phone'] = array('like',"%".trim($_REQUEST['phone'])."%");

        if( $_REQUEST['mobile'] != '' )
          $map['mobile'] = array('like',"%".trim($_REQUEST['mobile'])."%");

        if( $_REQUEST['address'] != '' )
          $map['address'] = array('like',"%".trim($_REQUEST['address'])."%");

        if( $_REQUEST['shipping_name'] != '' )
          $map['shipping_name'] = array('like',"%".trim($_REQUEST['shipping_name'])."%");

        if( $_REQUEST['customer_name'] != '' )
          $map['customer_name'] = array('like',"%".trim($_REQUEST['customer_name'])."%");


        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
                $where['Sale_detail.sale_title'] = array('like',"%".$key."%");//产品名称
                //$where['Sale.customer_name'] = array('like',"%".$key."%");//产品名称
                //$where['Sale.shipping_name'] = array('like',"%".$key."%"); 
                //$where['Sale.logino'] = array('like',"%".$key."%");
                //$where['Sale.address'] = array('like',"%".$key."%");
                //$where['Sale.mobile'] = array('like',"%".$key."%");
                //$where['Sale.phone'] = array('like',"%".$key."%");
                //$where['Sale.remark'] = array('like',"%".$key."%");
            
            //$where['_logic'] = "or";
            $map['_complex'] = $where;
        }
        if(!empty($_REQUEST['order_no'])) {
            $key2 = trim($_REQUEST['order_no']);
            if(!empty($_REQUEST['keywords']) || ($_REQUEST['etype'] == 'detail') )
              $map['_string'] = '(Sale.sale_no like "%'.$key2.'%")  OR ( Sale.mall_no like "%'.$key2.'%") ';
            else
              $map['_string'] = '(sale_no like "%'.$key2.'%")  OR ( mall_no like "%'.$key2.'%") ';
        }

        

        if( $_REQUEST['order_source_id'] != '' )
          $map['order_source_id'] = $_REQUEST['order_source_id'];
         if( $_REQUEST['logicompany_id'] != '' )
          $map['logicompany_id'] = $_REQUEST['logicompany_id'];
        
        //time 
        $cond_time = array();
        if( $_REQUEST['start_time'] != '' )
          array_push($cond_time, array( 'egt' , strtotime($_REQUEST['start_time']) ) );
          //$cond_time = array( 'egt' , strtotime($_REQUEST['start_time']) );
        if( $_REQUEST['end_time'] != '' )
          array_push($cond_time, array( 'elt' , strtotime($_REQUEST['end_time']) ) );

        //选择了时间类型,并且时间不为空，则按时间进行搜索
        if( $_REQUEST['time_type'] != "" && 
          ($_REQUEST['end_time'] != '' ||  $_REQUEST['start_time'] != '' ) ){
            switch ($_REQUEST['time_type']) {
              case '1': //审核时间
                $map['check_time '] = $cond_time;
                break;
              case '2': //发货时间
                $map['shipping_time '] = $cond_time;
                break;
              case '3': //下单时间
                if (empty($_REQUEST['keywords'])) {
                    
                    if($_REQUEST['etype'] == 'detail' ){ //导出订单详情，需按saleView进行搜索
                      $map['Sale.create_time '] = $cond_time;
                    }else
                      $map['create_time '] = $cond_time;
                }else
                    $map['Sale.create_time '] = $cond_time;
                break;
              case '4': //支付时间
                $map['pay_time '] = $cond_time;
                break;
              default:  //按下单时间查询
                $map['create_time '] = $cond_time;
                break;
            }
            
        }

        if( $_REQUEST['order_status'] != '' )
          $map['order_status'] = $_REQUEST['order_status'];
        else
            $map['order_status'] = 3; //shipped
        
        if( $_REQUEST['status'] != '' )
          $map['status'] = $_REQUEST['status'];
        else
            $map['status'] = 1; //checked
        //$map['prepare_status'] = 1; //货齐已打印
        
        if( $_REQUEST['warehouse_id'] == '' )
            $map['warehouse_id'] = 1; //默认同普仓库
        else
            $map['warehouse_id'] = $_REQUEST['warehouse_id'];
    }

     public function  index() {
        $map = $this->_search ();
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }
        if(empty($_REQUEST['keywords']) ){
          
          if(!empty($map)){
            $express_sum = M('Sale')->where($map)->count('distinct logino');
            //pass parameters to export_order function
            $parameters = str_replace(__URL__.'?', '', __SELF__);
            $this->assign('parameters' ,$parameters);
            $this->assign('express_sum' ,$express_sum); //统计发了多少票
          }
          parent::index('Sale');
        }else{ //view视图展示
            $model = D ('SaleView');
            if (! empty ( $model )) {
                $this->_list(D('SaleView'), $map, $sortBy = 'create_time desc', $asc = false,true);
            }
            $express_sum = M('Sale')->where($map)->count('distinct logino');
            //pass parameters to export_order function
            $parameters = str_replace(__ACTION__.'?', '', __SELF__);
            $this->assign('parameters' ,$parameters);
            dump($parameters);exit;
            $this->assign('express_sum' ,$express_sum); //统计发了多少票
            $this->display ();
            return;
        }

    }

   public function edit(){
	parent::edit('Sale');
   }
 
   public function update()
   {
    A('Sale')->update();
   }

}
?>
